package com.malbbmsomanager.retailmanagement;

import com.malbbmsomanagerdemo.R;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

public class DM_BBM_Retailer_Information extends Activity {
	private static final String TAG = null;
	EditText etretinfo_a, etretinfo_b, etretinfo_c, etretinfo_d, etretinfo_e,
	etretinfo_f, etretinfo_g, etretinfo_h, etretinfo_i,
	etretinfo_j, etretinfo_k,etretinfo_l,etretinfo_m,etretinfo_n,etretinfo_o;
	SQLiteDatabase mydb;
	String TagIdRetailer, printStatus;
	private String DBNAME = "dminvoicemanagerbbm.db";
	public void btnBack(View view) {
		Intent myIntent = new Intent(view.getContext(),
				DM_BBM_Retailer_Master.class);
		myIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP
				| Intent.FLAG_ACTIVITY_NEW_TASK);
		startActivityForResult(myIntent, 0);
		finish();
	}
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.dm_bbm_retailer_information);

		callCurrentRetailer();
		/*
		 * Bundle bundle = getIntent().getExtras(); TagIdRetailer =
		 * bundle.getString("TAGIDRET");
		 */
		Log.w(TAG, "TagIdRetailer:" + TagIdRetailer);

		TextView txtHeading = (TextView) findViewById(R.id.txtHeading);
		txtHeading.setText("Retailer Information");
		etretinfo_a = (EditText) findViewById(R.id.etretinfo_a);
		etretinfo_b = (EditText) findViewById(R.id.etretinfo_b);
		etretinfo_c = (EditText) findViewById(R.id.etretinfo_c);
		etretinfo_d = (EditText) findViewById(R.id.etretinfo_d);
		etretinfo_e = (EditText) findViewById(R.id.etretinfo_e);
		etretinfo_f = (EditText) findViewById(R.id.etretinfo_f);
		etretinfo_g = (EditText) findViewById(R.id.etretinfo_g);
		etretinfo_h = (EditText) findViewById(R.id.etretinfo_h);
		etretinfo_i = (EditText) findViewById(R.id.etretinfo_i);
		etretinfo_j = (EditText) findViewById(R.id.etretinfo_j);
		etretinfo_k = (EditText) findViewById(R.id.etretinfo_k);
		etretinfo_l = (EditText) findViewById(R.id.etretinfo_l);
		etretinfo_m = (EditText) findViewById(R.id.etretinfo_m);
		etretinfo_n = (EditText) findViewById(R.id.etretinfo_n);
		etretinfo_o = (EditText) findViewById(R.id.etretinfo_o);
		displayRetailerInformation();
	}

	private void callCurrentRetailer() {
		// TODO Auto-generated method stub
		mydb = openOrCreateDatabase(DBNAME, Context.MODE_PRIVATE, null);
		Cursor curCurrentRetailer = null;
		try {
			String query = "SELECT * from FRetailScrSum";
			curCurrentRetailer = mydb.rawQuery(query, null);
		} catch (Exception e) {
		}
		if (curCurrentRetailer != null) {
			if (curCurrentRetailer.getCount() > 0) {
				while (curCurrentRetailer.moveToNext()) {
					TagIdRetailer = curCurrentRetailer.getString(1);
				}
			}
		}
	}

	private void displayRetailerInformation() {
		// TODO Auto-generated method stub
		mydb = openOrCreateDatabase(DBNAME, Context.MODE_PRIVATE, null);
		Cursor curRetailerInfo = null;

		try {
			String query = "SELECT  DebCode,DebName,DebAdd1,DebAdd2,DebAdd3,DebTele,DebMob,DelEmail,Status,DelPersn,AreaCode,CrdPeriod,CrdLimit,RepCode,PrilCode from fDebtor WHERE DebCode = '" + TagIdRetailer + "'";
			curRetailerInfo = mydb.rawQuery(query, null);
		} catch (Exception e) {
		}
		if (curRetailerInfo != null) {
			if (curRetailerInfo.getCount() > 0) {
				while (curRetailerInfo.moveToNext()) {
					String DebCode = curRetailerInfo.getString(0);
					String DebName = curRetailerInfo.getString(1);
					String DebAdd1 = curRetailerInfo.getString(2);
					String DebAdd2 = curRetailerInfo.getString(3);
					String DebAdd3 = curRetailerInfo.getString(4);
					String DebTele = curRetailerInfo.getString(5);
					String DebMob = curRetailerInfo.getString(6);
					String DelEmail = curRetailerInfo.getString(7);
					String Status = curRetailerInfo.getString(8);
					if (Status.equals("A")) {
						printStatus = "Active";
					}
					else{
						printStatus = "Inactive";
					}
					String DelPersn = curRetailerInfo.getString(9);
					String AreaCode = curRetailerInfo.getString(10);
					String CrdPeriod = curRetailerInfo.getString(11);
					String CrdLimit = curRetailerInfo.getString(12);
					String RepCode = curRetailerInfo.getString(13);
					String PrilCode = curRetailerInfo.getString(14);

					etretinfo_a.setText(DebCode);
					etretinfo_b.setText(DebName);
					etretinfo_c.setText(DebAdd1);
					etretinfo_d.setText(DebAdd2);
					etretinfo_e.setText(DebAdd3);
					etretinfo_f.setText(DebTele);
					etretinfo_g.setText(DebMob);
					etretinfo_h.setText(DelEmail);
					etretinfo_i.setText(printStatus);
					etretinfo_j.setText(DelPersn);
					etretinfo_k.setText(AreaCode);
					etretinfo_l.setText(CrdPeriod);
					etretinfo_m.setText(CrdLimit);
					etretinfo_n.setText(RepCode);
					etretinfo_o.setText(PrilCode);
				}
			}
		}
		curRetailerInfo.close();
		mydb.close();
	}
	
	@Override
	public void onBackPressed() {
		// Do Here what ever you want do on back press;
	}
}
